var ctx = localStorage.getItem("ctx");
var wholeprovinceId = '';
var wholecityId = '';
var wholecompanyId = '';
var table = '';
var orderStatus = 0;
var timeStart = '';
var timeEnd = '';
var searchordername = '';
var searchorderaddress = '';
$(function() {
	
	juicer.set({
	    'tag::operationOpen': '-~',
	    'tag::operationClose': '+',
	    'tag::interpolateOpen': '#-',
	    'tag::interpolateClose': '+',
	    'tag::noneencodeOpen': '##-',
	    'tag::noneencodeClose': '+',
	    'tag::commentOpen': '-*',
	    'tag::commentClose': '+'
	});
	
	var uvId = '';
	var vrId = '';
	
	
	var isAdmin = localStorage.getItem('isAdmin');
	console.log(isAdmin);
	if(isAdmin == 'false') {
		$(".isAdminProvince").hide();
		$(".isAdminCity").hide();
		$(".isAdminCompany").hide();
		//非超级管理员获取公司列表
		getCompanyByOtherUser();
	}
	if(isAdmin == 'true') {
		$(".isOtherUserCompany").hide();
	}

	//获取订单列表表格
	orderTable(wholeprovinceId,wholecityId,wholecompanyId,orderStatus,timeStart,timeEnd,searchordername,searchorderaddress);
	
	
	
	//获取省份,城市，公司初始化
	getProvince();
	getCity('');
	getCompany('','');
	
	
	//根据选取的省份获取城市和公司
	$(".provinceSelect").on("change", function(){
		var provinceId = $(this).attr("value");
		getCity(provinceId);
		getCompany(provinceId,'');
	});
	
	//根据省份和城市获取公司
	$(".citySelect").on("change", function() {   	
		var provinceId = $(".provinceSelect").find("option:selected").attr("value");
		var cityId = $(this).attr("value");
		getCompany(provinceId,cityId);
	});
	
	
	
	
	//查询列表
	$(".search-btn").on("click", function() {
		
		wholeprovinceId = $(".provinceSelect").find("option:selected").attr("value");
    	wholecityId = $(".citySelect").find("option:selected").attr("value");
    	//判断是否是超级管理员
    	if(isAdmin == 'false') {
    		wholecompanyId = $(".otherUserCompanySelect").find("option:selected").attr("value");
    	}
    	else {
    		wholecompanyId = $(".companySelect").find("option:selected").attr("value");
    	}
//    	wholecompanyId = $(".companySelect").find("option:selected").attr("value");
    	orderStatus = $(".orderStatusSelect").find("option:selected").attr("value");
    	timeStart = $(".startDate").val();
    	timeEnd = $(".endDate").val();
    	searchordername = $(".searchordername").val();
    	searchorderaddress = $(".searchorderaddress").val();
    	table.destroy();
		//查询获取订单列表表格
    	orderTable(wholeprovinceId,wholecityId,wholecompanyId,orderStatus,timeStart,timeEnd,searchordername,searchorderaddress);
    		
		
		
	});
	
	
	
	//查看详情模态框
	$(document).on("click", ".btn-detail", function() {
		var data = table.row( $(this).parents('tr') ).data();
		var oid = data.oid;
		console.log(oid);
		$.ajax({
			
			type: 'GET',
			url: ctx+'/order/details',
			dataType: 'json',
			data: {
				oid: oid
			},
			success: function(data) {
				
				console.log(data);
				
				$(".passangerTel").html(data.passengerPhone == null || '' ? "暂无" : data.passengerPhone);//乘客手机号
				$(".oid").html(data.oid == null || '' ? "暂无" : data.oid);//订单号
				$(".payTimeStr").html(data.payTime == null || '' ? "暂无" : data.payTime);//结束时间和订单支付时间
				$(".isDispay").html(data.isDispay == null || '' ? "暂无" : data.isDispay);//是否评论
				$(".placeOrderUserId").html(data.passengerName == null || '' ? "暂无" : data.passengerName);//下单用户
				$(".duration").html(data.duration == null || '' ? "暂无" : data.duration);//订单时长
				$(".evaluateDateStr").html(data.evaluateDate == null || '' ? "暂无" : data.evaluateDate);//评论时间
				$(".grabOrderUserId").html(data.driverName == null || '' ? "暂无" : data.driverName);//抢单司机
				//出发类型
				if(data.setOutType == 1) {
					$(".setOutType").html("实时");
				}
				else if(data.setOutType == 2) {
					$(".setOutType").html("预约");
				}
				else {
					$(".setOutType").html("暂无");
				}
				$(".evaluateLevel").html(data.evaluateLevel == null || '' ? "暂无" : data.evaluateLevel);//星级
				//订单状态
				if(data.orderSate == 1) {
					$(".orderStatus").html("待指派");
				}
				else if(data.orderSate == 2) {
					$(".orderStatus").html("待上车(已指派)");
				}
				else if(data.orderSate == 3) {
					$(".orderStatus").html("已上车");
				}
				else if(data.orderSate == 4) {
					$(".orderStatus").html("待支付(已到达)");
				}
				else if(data.orderSate == 5) {
					$(".orderStatus").html("已完成");
				}
				else if(data.orderSate == 6) {
					$(".orderStatus").html("已取消");
				}
				else {
					$(".orderStatus").html("暂无");
				}
				//取消类型
				if(data.cancelType == 1) {
					$(".cancelType").html("乘客取消");
				}
				else if(data.cancelType == 2) {
					$(".cancelType").html("车主取消");
				}
				else if(data.cancelType == 3) {
					$(".cancelType").html("自动取消");
				}
				else {
					$(".cancelType").html("暂无");
				}
				$(".evaluateLabel").html(data.evaluateLabel == null || data.evaluateLabel.length < 1 ? "暂无" : data.evaluateLabel);//评论标签
				$(".createTimeStr").html(data.createTime == null || '' ? "暂无" : data.createTime);//下单时间
				$(".cancelTimeStr").html(data.cancelTime == null || '' ? "暂无" : data.cancelTime);//订单取消时间
				$(".evaluateContent").html(data.evaluateContent == null || data.evaluateLabel.length < 1 ? "暂无" : data.evaluateContent);//评论内容
				$(".placeOrderCity").html(data.placeOrderCity == null || '' ? "暂无" : data.placeOrderCity);//下单城市
				$(".cancelDesc").html(data.cancelDesc.length > 1 ? data.cancelDesc : '暂无');//订单取消原因
				//发单类型
				if(data.publishType == 1) {
					$(".publishType").html("顺风车");
				}
				else if(data.publishType == 2) {
					$(".publishType").html("出租车");
				}
				else if(data.publishType == 3) {
					$(".publishType").html("专车");
				}
				else if(data.publishType == 4) {
					$(".publishType").html("渠道发单(专车)");
				}
				else {
					$(".publishType").html("暂无");
				}
				//出发条件
				if(data.setOutCondition == 1) {
					$(".setOutCondition").html('无');
				}
				else if(data.setOutCondition == 2) {
					$(".setOutCondition").html('出发前后15分钟');
				}
				else if(data.setOutCondition == 3) {
					$(".setOutCondition").html('随时出发');
				}
				else {
					$(".setOutCondition").html('暂无');
				}
				$(".setOutTimeStr").html(data.setOutTime == null || '' ? "暂无" : data.setOutTime);//出发时间
				$(".appointTimeStr").html(data.appointTime == null || '' ? "暂无" : data.appointTime);//指派时间
				$(".startAddress").html(data.startAddress == null || '' ? "暂无" : data.startAddress);//起点
				$(".aboardLatitude").html(data.aboardLatitude == null || '' ? '暂无' : data.aboardLatitude);//上车纬度
				$(".estimateDistance").html(data.estimateDistance == null || '' ? '暂无' : data.estimateDistance);//平台估算距离
				$(".startLongitude").html(data.startLongitude == null || '' ? '暂无' : data.startLongitude);//起点经度
				$(".debusLongitude").html(data.debusLongitude == null || '' ? '暂无' : data.debusLongitude);//下车经度
				$(".estimateDuration").html(data.estimateDuration == null || '' ? '暂无' : data.estimateDuration);//平台估算时长
				$(".startLatitude").html(data.startLatitude == null || '' ? '暂无' : data.startLatitude);//起点纬度
				$(".debusLatitude").html(data.debusLatitude == null || '' ? '暂无' : data.debusLatitude);//下车纬度
				$(".endAddress").html(data.endAddress == null || '' ? '暂无' : data.endAddress);//终点
				$(".aboardTime").html(data.aboardTime == null || '' ? '暂无' : data.aboardTime);//上车时间
				$(".endLongitude").html(data.endLongitude == null || '' ? '暂无' : data.endLongitude);//终点经度
				$(".distance").html(data.distance == null || '' ? '暂无' : data.distance);//行程距离
				$(".endLatitude").html(data.endLatitude == null || '' ? '暂无' : data.endLatitude);//终点纬度
				$(".endTimeStr").html(data.endTime == null || '' ? '暂无' : data.endTime);//车主确认到底时间
				$(".aboardLongitude").html(data.aboardLongitude == null || '' ? '暂无' : data.aboardLongitude);//上车经度
				$(".totalMoney").html(data.totalMoney == null || '' ? '暂无' : data.totalMoney);//订单总金额
				$(".startFee").html(data.startFee == null || '' ? '暂无' : data.startFee);//起步费
				$(".payMoney").html(data.payMoney == null || '' ? '暂无' : data.payMoney);//叮币支付金额
				$(".durationFee").html(data.durationFee == null || '' ? '暂无' : data.durationFee);//时长费
				$(".alipayMoney").html(data.alipayMoney == null || '' ? '暂无' : data.alipayMoney);//第三方支付金额
				$(".mileageFee").html(data.mileageFee == null || '' ? '暂无' : data.mileageFee);//里程费
				//支付类型
				if(data.payType == 1) {
					$(".payType").html("支付宝支付");
				}
				else if(data.payType == 2) {
					$(".payType").html("微信支付");
				}
				else if(data.payType == 3) {
					$(".payType").html("叮币支付");
				}
				else {
					$(".payType").html("暂无");
				}
				$(".minimums").html(data.minimums == null || '' ? '暂无' : data.minimums);//最低消费
//				$(".payTimeStr").html(data.payTime);//订单支付时间
				$(".longFee").html(data.longFee == null || '' ? '暂无' : data.longFee);//远途费
				//交易类型
				if(data.transactionType == 1) {
					$(".transactionType").html("线上支付");
				}
				else if(data.transactionType == 2) {
					$(".transactionType").html("线下支付");
				}
				else {
					$(".transactionType").html("暂无");
				}
				$(".nightFee").html(data.nightFee == null || '' ? '暂无' : data.nightFee);//夜间费用
				$(".estimateMoney").html(data.estimateMoney == null || '' ? '暂无' : data.estimateMoney);//平台估算金额
				
				$("#myModalDetail").modal('show');
			}
			
		});
	});
	
	
	
	//获取省份
    function getProvince() {
    	$.ajax({
        	type: 'POST',
    		url: ctx+'/proandcity/v1/pro',
    		dataType: 'json',
    		success: function(data) {
    			
    			var provinceTpl = document.getElementById("provinceSelectTpl").innerHTML;
    			var provincehtml = juicer(provinceTpl, data);
    			var newprovincehtml = "<option value=''>全部</option>" + provincehtml;
    			$(".provinceSelect").html(newprovincehtml);
    			
    		}
    	});
    }
    
	
    //根据省份获取城市(可以传空，代表全部)
    function getCity(provinceId) {
    	$.ajax({
        	type: 'POST',
    		url: ctx+'/proandcity/v1/city',
    		dataType: 'json',
    		data: {
    			proviceId: provinceId
    		},
    		success: function(data) {
    			var cityTpl = document.getElementById("citySelectTpl").innerHTML;
    			var cityhtml = juicer(cityTpl, data);
    			var newcityhtml = "<option value=''>全部</option>" + cityhtml;
    			$(".citySelect").html(newcityhtml);
    			
    		}
    	});
    }
    
    
    //根据省份和城市获取公司(可以传空，代表全部)
    function getCompany(provinceId,cityId) {
    	$.ajax({
    		type: 'POST',
    		url: ctx+'/sys/v1/organization/listId',
    		dataType: 'json',
    		data: {
    			proviceId: provinceId,
    			sysCityId: cityId
    		},
    		success: function(data) {
    			var company = {};
    			company.list = data;
    			var companyTpl = document.getElementById("companySelectTpl").innerHTML;
    			var companyhtml = juicer(companyTpl, company);
    			var newcompanyhtml = "<option value=''>全部</option>" + companyhtml;
    			$(".companySelect").html(newcompanyhtml);
    		}
    	});
    }
	
    //获取非超级管理员用户所能访问的公司列表
    function getCompanyByOtherUser() {
    	$.ajax({
    		type: 'POST',
    		url: ctx+'/sys/v1/organization/organizationList',
    		dataType: 'json',
    		success: function(data) {
    			console.log(data);
    			var company = {};
    			company.list = data;
    			var companyTpl = document.getElementById("otherUserCompanySelectTpl").innerHTML;
    			var companyhtml = juicer(companyTpl, company);
    			var newcompanyhtml = "<option value=''>全部</option>" + companyhtml;
    			$(".otherUserCompanySelect").html(newcompanyhtml);
    		}
    	});
    }
	
	
	
})
